package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.SubwayBean;
import sz.szsmk.citizencard.model.network.bean.response.SubwayLineBean;
import sz.szsmk.citizencard.ui.adapter.SubwayHomeAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.CommonUtils;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.MesageEventBus;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/11 15:10
 * description:地铁首页
 */
@Route("SubwayHomeActivity")
public class SubwayHomeActivity extends IBaseActivity implements View.OnClickListener {
    LinearLayout lay;
    RecyclerView subwayHomeRv;
    TextView realTimeStart;
    TextView realTimeEnd;
    ImageView back;
    ImageView real_time_more;
    TextView subwayLineName;
    TextView subwaySiteSize;
    TextView subwayHomeInputStart;
    TextView subwayHomeInputEnd;
    TextView queryLine;
    ImageView subwaychange;

    private ArrayList<String> subwayLine = new ArrayList<>();

    TabLayout tabs_lines;

    private SubwayLineBean subwayLineBean;

    private SubwayHomeAdapter subwayHomeAdapter;

    private int currentLine;

    @Override
    protected int attachLayoutRes() {
        return R.layout.subway_home_activity;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {



        lay = findViewById(R.id.lay);
        subwayHomeRv = findViewById(R.id.subway_home_rv);
        realTimeStart = findViewById(R.id.real_time_start);
        realTimeEnd= findViewById(R.id.real_time_end);
        back = findViewById(R.id.back);

        back.setOnClickListener(this);

        real_time_more = findViewById(R.id.real_time_more);
        subwayLineName = findViewById(R.id.subway_line_name);
        subwaySiteSize = findViewById(R.id.subway_site_size);
        subwayHomeInputStart = findViewById(R.id.subway_home_input_start);
        subwayHomeInputStart.setOnClickListener(this);
        subwayHomeInputEnd = findViewById(R.id.subway_home_input_end);
        subwayHomeInputEnd.setOnClickListener(this);
        queryLine = findViewById(R.id.query_line);
        queryLine.setOnClickListener(this);
        subwaychange = findViewById(R.id.subwaychange);
        subwaychange.setOnClickListener(this);

        findViewById(R.id.real_time_more).setOnClickListener(this);



        EventBus.getDefault().register(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        //这里有2个json文件 一个是 地铁线路的json 一个是地铁站点的json
        //地铁站点的json 是百度复制下来的  由于4号线支线 百度地图sdk 搜索 4号线支线   返回结果全是null   改为json文件形式
        //三号线 一号线 二号线 。。 是需求如此
        //所以下面的 索引也会改变
        //根据 szSubway.json 文件读取   三号线的索引 是0    一号线的索引是 1 。。
        //如果增加新线路   放在了索引的第一个  那么两个json文件都要改动
        //szSubway.json 与 subway.json  路线和 站点是对应的
        //szSubway.json 的第0个 是三号线    subway.json 的第0个 三号线的站点

        String json = CommonUtils.getJson(this, "szSubway.json");
        Gson gson = new Gson();
        subwayLine = (ArrayList<String>) gson.fromJson(json, SubwayBean.class).getSubway_list();
        for(int i=0;i<subwayLine.size();i++){
            TabLayout.Tab tab = tabs_lines.newTab().setText(subwayLine.get(i));
            tabs_lines.addTab(tab);
        }

        String subwayjson = CommonUtils.getJson(this, "subway.json");
        subwayLineBean = new Gson().fromJson(subwayjson, SubwayLineBean.class);

        subwayHomeAdapter = new SubwayHomeAdapter(this);
        subwayHomeRv.setAdapter(subwayHomeAdapter);
        subwayHomeRv.setLayoutManager(new LinearLayoutManager(this));

        currentLine = 0;
        data = subwayLineBean.getLine_list().get(currentLine).getG();
        loadLine(currentLine);

        tabs_lines.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String currentName = tab.getText().toString();
                currentLine = tab.getPosition();
//                "三号线","一号线","二号线","四号线","四号线支线"
                switch (tab.getText().toString()){
                    case "一号线":
                        currentLine = 1;
                        break;
                    case "二号线":
                        currentLine = 2;
                        break;
                    case "三号线":
                        currentLine = 0;
                        break;
                    case "四号线":
                        currentLine = 3;
                        break;
                    case "四号线支线":
                        currentLine = 4;
                        break;
                }
                data = subwayLineBean.getLine_list().get(currentLine).getG();
                loadLine(currentLine);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



    }

    private double startLat;
    private double startLog;
    private double endLat;
    private double endLog;

    @Override
    protected void updateViews() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MesageEventBus eventBus) {
        if (eventBus.getStart() != null) {

            subwayHomeInputStart.setText(eventBus.getStart());
            startLat = eventBus.getLat();
            startLog = eventBus.getLog();
        }
        if (eventBus.getEnd() != null) {
            subwayHomeInputEnd.setText(eventBus.getEnd());
            endLat = eventBus.getLat();
            endLog = eventBus.getLog();
        }
    }

    List<SubwayLineBean.LineListBean.GBean> data ;
    public void loadLine(int line){
        int trueCurrentLine = currentLine==0?3:currentLine;

        subwayLineName.setText( data.get(0).getA()  +"-"+data.get(data.size()-1).getA() );
        subwaySiteSize.setText("全程" + data.size() + "站");

        //起止时间
        SubwayLineBean.LineListBean lineListBean =  subwayLineBean.getLine_list().get(line);
        String e = lineListBean.getE();
        e = e.substring(e.length() - 8);
        String d = lineListBean.getD();
        d = d.substring(d.length() - 8);
        Date date = strToDateLong(e);
        Date date1 = strToDateLong(d);

        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        String dateString = formatter.format(date);
        String dateString1 = formatter.format(date1);

        realTimeEnd.setText(dateString);
        realTimeStart.setText(dateString1);
    }


    public void onClick1(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.subway_home_input_start) {
            Bundle bundle = new Bundle();
            bundle.putString("type", "start");
            JumpUtils.jumptoNatvie(this, "SubwayStartActivity", 0, bundle);
        } else if (id == R.id.subway_home_input_end) {
            Bundle bundle1 = new Bundle();
            bundle1.putString("type", "end");
            JumpUtils.jumptoNatvie(this, "SubwayStartActivity", 0, bundle1);
        } else if (id == R.id.query_line) {
            String start = subwayHomeInputStart.getText().toString().trim();
            String end = subwayHomeInputEnd.getText().toString().trim();
            if (start.equals("选择起点")) {
                Toast.makeText(this, "请选择起点", Toast.LENGTH_SHORT).show();
                return;
            }
            if (end.equals("选择终点")) {
                Toast.makeText(this, "请选择终点", Toast.LENGTH_SHORT).show();
                return;
            }
            if (start.equals(end)) {
                Toast.makeText(this, "请选择不同的起点站和终点站", Toast.LENGTH_SHORT).show();
                return;
            }
            Bundle bundles = new Bundle();
            bundles.putDouble("startlat", startLat);
            bundles.putDouble("startlng", startLog);
            bundles.putDouble("endlat", endLat);
            bundles.putDouble("endlng", endLog);
            bundles.putString("start", start);
            bundles.putString("end", end);
            bundles.putString("line", currentLine + "");
            JumpUtils.jumptoNatvie(SubwayHomeActivity.this, "BusChangeActivity", 0, bundles);
        } else if (id == R.id.real_time_more) {//顺序反转
            Collections.reverse(data);
            loadLine(currentLine);
        } else if (id == R.id.subwaychange) {
            String startStr = subwayHomeInputStart.getText().toString();
            String endStr = subwayHomeInputEnd.getText().toString();
            subwayHomeInputStart.setText(endStr);
            subwayHomeInputEnd.setText(startStr);
            double tempstartlat = startLat;
            double tempendlog = startLog;

            startLat = endLat;
            startLog = endLog;
            endLat = tempstartlat;
            endLog = tempendlog;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    @Override
    public void onClick(View v) {
        onClick1(v);
    }
}
